package cn.hist.hfp.exception;

import org.apache.tomcat.util.http.fileupload.FileUploadBase;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

@ControllerAdvice
@ResponseBody
public class DefaultExceptionHandler {

    @ResponseStatus(HttpStatus.OK)
    @ExceptionHandler(ResourceNotFoundException.class)
    public ErrorVO resourceNotFound(ResourceNotFoundException e) {
        return new ErrorVO(ErrorCode.RESOURCE_NOT_FOUND);
    }

    @ResponseStatus(HttpStatus.OK)
    @ExceptionHandler(BusinessException.class)
    public ErrorVO businessException(BusinessException e) {
        System.err.println(e.getErrorCode().getMessage());
        return new ErrorVO(e.getErrorCode());
    }

    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ExceptionHandler(InternalException.class)
    public ErrorVO internalException(InternalException e) {
        System.err.println(e.getErrorCode().getMessage());
        return new ErrorVO(e.getErrorCode());
    }

    @ResponseStatus(HttpStatus.OK)
    @ExceptionHandler(StorageFileNotFoundException.class)
    public ResponseEntity<?> handleStorageFileNotFound(StorageFileNotFoundException exc) {
        return ResponseEntity.notFound().build();
    }

    @ResponseStatus(HttpStatus.OK)
    @ExceptionHandler(FileUploadBase.SizeLimitExceededException.class)
    public ErrorVO sizeLimitExceededException(FileUploadBase.SizeLimitExceededException e) {
        return new ErrorVO(ErrorCode.SIZE_LIMIT_EXCEEDED);
    }
}
